package com.grey_zoo.lottery.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.grey_zoo.lottery.R;
import com.grey_zoo.lottery.bean.BmobLotteryData;
import com.grey_zoo.lottery.engin.UserEngin;
import com.grey_zoo.lottery.eventhandler.BombEventHandler;
import com.grey_zoo.lottery.global.GlobalValue;
import com.grey_zoo.lottery.presenter.BombEventManager.MyLotteryCheck;
import com.grey_zoo.lottery.util.JSONUtils;
import com.grey_zoo.lottery.util.PromptManager;
import com.grey_zoo.lottery.view.manager.MiddleManager;

public class DetailLotteryUI extends BaseUI {

	private ListView lv;

	public static int index;

	private BaseAdapter adapter = new MyBaseAdapter();

	/**
	 * 设置数据
	 */
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			adapter.notifyDataSetChanged();

			PromptManager.closeProgressDialog();

		};
	};

	/**
	 * 期数
	 */
	private String qs;

	/**
	 * 时间
	 */
	private String time;

	/**
	 * 用户事务处理
	 */
	private UserEngin engin;

	/**
	 * blue的list
	 */
	private List<String> list_blue = new ArrayList<String>();

	/**
	 * red的list
	 */
	private List<String> list_red = new ArrayList<String>();

	public DetailLotteryUI(Context context) {
		super(context);
		// TODO 自动生成的构造函数存根
	}

	@Override
	public void onClick(View v) {
		// TODO 自动生成的方法存根

	}

	@Override
	public View createView() {
		view = View.inflate(context, R.layout.il_detaillottery, null);
		view.setLayoutParams(new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));

		init();

		return view;
	}

	private void init() {
		// TODO 自动生成的方法存根
		lv = (ListView) view.findViewById(R.id.lv);

		qs = (String) MiddleManager.getInstance().bundle.get("qs");
		time = (String) MiddleManager.getInstance().bundle.get("time");

		// engin = new UserEngin();

		getDataFromNet(qs, time);

		lv.setAdapter(adapter);

	}

	private void getDataFromNet(String qs, String time) {
		// TODO 自动生成的方法存根
		PromptManager.showProgressDialog(GlobalValue.activity);
		/*
		 * engin.GetDetailLottery(new MyLotteryCheck() {
		 * 
		 * @Override public void onSuuccd(List<BmobLotteryData> arg0) { if
		 * (arg0.size() > 0) { // 成功获取数据，进行加载显示 BmobLotteryData data =
		 * arg0.get(0); JSONUtils.getDetailLotteryFromJSON(data.getBlue(),
		 * data.getRed(), list_blue, list_red); handler.sendEmptyMessage(0); }
		 * else {
		 * 
		 * } }
		 * 
		 * @Override public void onFail() { // TODO 自动生成的方法存根
		 * 
		 * } }, qs, time);
		 */

		BombEventHandler.BombGetDetailLotteryEventHandler(6,
				new MyLotteryCheck() {

					@Override
					public void onSuuccd(List<BmobLotteryData> arg0) {
						if (arg0.size() > 0) {
							// 成功获取数据，进行加载显示
							BmobLotteryData data = arg0.get(0);
							JSONUtils.getDetailLotteryFromJSON(data.getBlue(),
									data.getRed(), list_blue, list_red);
							handler.sendEmptyMessage(0);
						} else {

						}
					}

					@Override
					public void onFail() {
						// TODO 自动生成的方法存根

					}
				}, qs, time);
	}

	@Override
	public Integer getId() {
		// TODO 自动生成的方法存根
		return GlobalValue.DETAILLOTTERY;
	}

	@Override
	public void OnPause() {
		// TODO 自动生成的方法存根

	}

	@Override
	public void OnResume() {
		// TODO 自动生成的方法存根

	}

	class MyBaseAdapter extends BaseAdapter {

		private LayoutInflater inflater = LayoutInflater.from(context);

		// inflater = LayoutInflater.from(context);
		@Override
		public int getCount() {
			// TODO 自动生成的方法存根
			return list_blue.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO 自动生成的方法存根
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO 自动生成的方法存根
			return 0;
		}

		@Override
		public View getView(int position, View convert, ViewGroup parent) {
			// TODO 自动生成的方法存根
			index = position;
			ViewHolder holder;
			if (convert == null) {
				// 调用LayoutInflater的inflate方法加载layout文件夹中的界面
				convert = inflater.inflate(R.layout.fenxiang_ssq, null);
				holder = new ViewHolder();
				holder.data_text = (TextView) convert
						.findViewById(R.id.data_text);
				holder.fenxiangbutton = (Button) convert
						.findViewById(R.id.button_fenxiang);
				// 保存包含当前项控件的对象
				convert.setTag(holder);
			} else {
				// 获取包含当前项控件的对象
				holder = (ViewHolder) convert.getTag();
			}

			holder.data_text.setText(list_red.get(position) + " :"
					+ list_blue.get(position));
			holder.fenxiangbutton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					// TODO Auto-generated method stub

					StartShareApp(GlobalValue.activity, "分享到", "分享到",
							"我买的这期彩票号为" + list_red.get(index) + " : "
									+ list_blue.get(index) + "大家祝我中大奖吧");

				}

			});
			return convert;
		}

	}

	class ViewHolder {
		TextView data_text;

		Button fenxiangbutton;
	}

	static public void StartShareApp(Context context,
			final String szChooserTitle, final String title, final String msg) {
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_SUBJECT, title);
		intent.putExtra(Intent.EXTRA_TEXT, msg);
		context.startActivity(Intent.createChooser(intent, szChooserTitle));
	}

}
